//es6 解决异步编程的一种方案
//1.定义generator函数 function函数名之间需要有星号  2.yield表达式 


// function* helloGenerator(){
//     console.log("111111")
//     yield 'hello';
//     console.log("22222")
//     yield 'world';
//     console.log("333333")
//     return 'end'
// }

// let hello = helloGenerator();

// console.log(hello.next()); //next碰到yield会自动停止  {value: "hello", done: false}

// console.log(hello.next());

// console.log(hello.next());

// console.log(hello.next());

// for(let i of hello){  //不会取到return的值
//     console.log(i); 
// }


// async function fun(){
//     await
//     await
// }

// fun()


//把大象关进冰箱

//1.打开冰箱门

function first(fn){
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            console.log("打开冰箱门");
            fn();//执行下一步
            resolve();
        },500)
    })
}

//2.把大象放进冰箱

function second(fn){
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            console.log("把大象放进冰箱");
            fn();//执行下一步
            resolve();
        },500)
    })
}

//3.关上冰箱门

function three(fn){
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            console.log("关上冰箱门");
            fn();//执行下一步
            resolve();
        },500)
    })
}

function* task(){
    yield first;
    yield second;
    yield three;
}

// let gen = task();

// console.log(gen.next());

// console.log(gen.next());

// console.log(gen.next());

// console.log(gen.next());

//自执行

function run(task){
    let gen = task();

    let next = () => {
        let result = gen.next();///{value:function,done:true}
        if(result.done){
            return 
        }
        result.value(next)
    }
    next();
}

run(task)


